from rest_framework import serializers

from goods.models import SPU, Brand, GoodsCategory


class SPUSerializer(serializers.ModelSerializer):
    """SPU商品详情序列化器类"""
    brand = serializers.StringRelatedField(label='SPU名称')
    brand_id = serializers.IntegerField(label='SPU ID')
    category1_id = serializers.IntegerField(label='一级分类')
    category2_id = serializers.IntegerField(label='二级分类')
    category3_id = serializers.IntegerField(label='三级分类')
    category1 = serializers.StringRelatedField(label='一级分类')
    category2 = serializers.StringRelatedField(label='二级分类')
    category3 = serializers.StringRelatedField(label='三级分类')

    class Meta:
        model = SPU
        # fields = ('id', 'name', 'category1', 'category2', 'category3', 'sales', 'comments', 'brand', 'brand_id')
        fields = '__all__'

class BrandSimpleSerializer(serializers.ModelSerializer):
    """品牌简单展示"""
    class Meta:
        model = Brand
        fields = ('id', 'name')


class CategoriesSimpleSerializer(serializers.ModelSerializer):
    """一级分类简单展示"""

    class Meta:
        model = GoodsCategory
        fields = ('id','name')


class SimpleSerializer(serializers.ModelSerializer):
    """二 三级分类简单展示"""
    subs = CategoriesSimpleSerializer(label='二级和三级分类数据',many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id','name','subs')
